<?php
/**
 * 短信验证码管理
 * Created by gii.
 * User: Gii
 * Date: 2023/02/23
 * Time: 16:16
 */

namespace app\admin\controller\system;

use app\admin\logic\system\UserCodeLogic;
use app\Request;
use think\App;

class UserCodeController extends BaseController
{

    public $title = '短信验证码';

    public function __construct(App $app)
    {
        parent::__construct($app);
    }

    // 表格页面
    public function index() {
        $search = [
            [
                'type'  => 'input',
                'key'   => 'id',
                'name'  => '请输入ID',
            ],
        ];
        $searchHtml = makeSearch($search, []);
        $this->assign('search', $searchHtml);
        return $this->fetch();
    }

    // 获取数据
    public function getData(Request $request) {
        $page = $request->get('page', 1);
        $pageSize = $request->get('limit', 10);
        $search = $request->get('key', []);

        $data = UserCodeLogic::getListWithCount($search, '*', $page, $pageSize);
        return $this->setSuccess($data->getList(), $data->getCount());
    }

    // 添加
    public function add(Request $request) {
        if($post = $request->post()) {
            $res = UserCodeLogic::add($post);
            if($res) {
                return $this->setSuccess();
            } else {
                return $this->setError(\Debug::getDebug());
            }
        }

        return $this->fetch(['get'=>[]], 'form');
    }

    // 编辑
    public function update(Request $request) {
        if($post = $request->post()) {
            $res = UserCodeLogic::update($post);
            if($res === false) {
                return $this->setError(\Debug::getDebug());
            } else {
                return $this->setSuccess();
            }
        }

        $id = $request->get('id');
        $get = UserCodeLogic::getOne($id);

        return $this->fetch(['get'=>$get], 'form');
    }

    // 删除
    public function del(Request $request) {
        if(!$id = $request->get('id')) {
            return $this->setError('请提交要删除的数据');
        }

        $res = UserCodeLogic::del($id);
        if(!$res) {
            return $this->setError('删除失败');
        }
        return $this->setSuccess();
    }

    // 状态
    public function status(Request $request) {
        if(!$id = $request->get('id')) {
            return $this->setError('请选择要修改的数据');
        }
        $status = $request->get('status');
        $res = UserCodeLogic::status($id, $status);
        if(!$res) {
            return $this->setError('操作失败');
        }
        return $this->setSuccess();
    }

}